﻿using System;
using System.Web.UI.HtmlControls;
using HNAlbum.Helper;
using HNAlbum.WebControl;

namespace HNAlbum
{
    public partial class HNAlbum : System.Web.UI.MasterPage
    {
        #region Properties
        //Session manager
        private SessionManager _sessionCollection = new SessionManager();
        public SessionManager SessionCollection
        {
            get { return _sessionCollection; }
            set { _sessionCollection = value; }
        }
        #endregion

        #region Private helpers
        /// <summary>
        /// Authenticated current user
        /// </summary>
        private void checkUserAuthentication()
        {
            //Unauthenticated, redirect to login page
            if (!SessionCollection.User.IsAuthenticated)
            {
                //Redirect to login page always
                if (!Request.Url.LocalPath.Contains("Login.aspx"))
                    Response.Redirect(Constant.PageURL.Login);
            }
            else
            {
                //Not a guest
                if (SessionCollection.User.CurrentUser != null)
                {
                    //Manage account
                    btnManageAccount.Visible = true;

                    //Update welcome label
                    lblWelcome.Text = "Welcome, " + SessionCollection.User.CurrentUser.Name;

                    //Admin role, enable manage album
                    if (SessionCollection.User.CurrentUser.Role == Logic.Entity.HNAlbumUser.SecurityRole.Administrator)
                    {
                        btnManageAlbum.Visible = true;
                    }
                }
                else
                {
                    lblWelcome.Text = "Welcome, Guest";
                }
                //Show functional button
                btnLogout.Visible = true;

                //Redirect to home
                if (Request.Url.LocalPath.Contains("Login.aspx"))
                    Response.Redirect(Constant.PageURL.Home);
            }
        }
        #endregion

        #region Public methods
        /// <summary>
        /// Set page message
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        public void SetPageMessage(string msg, PageMessage.MessageType type)
        {
            pageMessage.ShowMessage(msg, type);
        }

        /// <summary>
        /// Set application theme
        /// </summary>
        /// <param name="themeName"></param>
        public void SetTheme(string themeName)
        {
            HtmlLink cssLink = new HtmlLink();
            //cssLink.Href = ResolveClientUrl(string.Format("~/CSS/Themes/{0}/theme.css", themeName));
            cssLink.Href = string.Format("~/CSS/Themes/{0}/theme.css", themeName);
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(cssLink);
        }

        /// <summary>
        /// Register CSS file
        /// </summary>
        /// <param name="fileName"></param>
        public void RegisterCSS(string fileName)
        {
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = string.Format("~/CSS/{0}", fileName);
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(cssLink);
        }

        /// <summary>
        /// Register Javascript file
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="folder"></param>
        public void RegisterJS(string fileName, string folder)
        {
            Page.ClientScript.RegisterClientScriptInclude(fileName, ResolveClientUrl(string.Format("~/JScript/{0}/{1}", folder, fileName)));
        }
        #endregion

        #region Form events

        protected void Page_Init(object sender, EventArgs e)
        {
            //Default theme
            SetTheme("doublehappiness");

            //Register common JS
            RegisterJS("jquery-1.6.1.min.js", "");
            RegisterJS("jquery-ui-1.8.11.custom.min.js", "");
            RegisterJS("jquery.blockUI.js", "");
            RegisterJS("jquery.easing.1.3.js", "");
            RegisterJS("jquery.color.js", "");
            RegisterJS("jquery.gritter.min.js", "");
            RegisterJS("jquery.hn.extend.js", "");
            RegisterJS("master.Page.js", "");

            //Register common CSS
            RegisterCSS("jquery.gritter.css");
            RegisterCSS("jquery.hn.extend.css");
        }
        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //Register CSS
            RegisterCSS("main.css");

            lblVersion.Text = "version: " + typeof(HNAlbum).Assembly.GetName().Version.ToString();
            //Authenticate current user
            checkUserAuthentication();
        }
        /// <summary>
        /// Handle logging out
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            //Unauthenticate the user
            SessionCollection.User.IsAuthenticated = false;
            SessionCollection.User.CurrentUser = null;
            //Redirect to login page
            Response.Redirect(Constant.PageURL.Login);
        }
        /// <summary>
        /// Redirect to Manage Album
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnManageAlbum_Click(object sender, EventArgs e)
        {
            Response.Redirect(Constant.PageURL.ManageAlbum);
        }
        /// <summary>
        /// Redirect to home page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnHome_Click(object sender, EventArgs e)
        {
            Response.Redirect(Constant.PageURL.Home);
        }
        #endregion
    }
}